/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package towerz.system;

import java.io.IOException;
import java.io.InputStream;
import javax.microedition.media.Manager;
import javax.microedition.media.MediaException;
import javax.microedition.media.Player;
import javax.microedition.media.PlayerListener;
import javax.microedition.media.control.VolumeControl;

/**
 *
 * @author TOSHIBA
 */
public class MediaPlayer implements PlayerListener {
    //Attributes
    private Player player;
    private InputStream is;
	private int m_repeat;
    private boolean isMute;
    private int volumeLevel;
	private VolumeControl vc;
	protected String m_Music;

    public MediaPlayer(){
		//Set default value
		isMute		= false;
		volumeLevel	= 50;
		m_Music		= null;
    }

    public void playerUpdate(Player player, String event, Object data) {
        if(event.equals(PlayerListener.END_OF_MEDIA)) {
			if(m_repeat == -1)
				play();
			else if(m_repeat > 0){
				play();
				--m_repeat;
			} else {
				/*try {
					defPlayer();
				} catch(MediaException me) {

				}
				reset();*/
			}
        }
    }

    public void load(String fileName, String fileType, int repeat) {
		//Stop if exist
		if (m_Music != null && !m_Music.equals(fileName))		stop();
		else if (m_Music != null && m_Music.equals(fileName))	return;
		
        try {
			m_repeat = repeat;

            //VolumeControl vc;
            defPlayer();

            //Create a player instance
            is = getClass().getResourceAsStream(fileName);
            player = Manager.createPlayer(is, fileType);
            player.addPlayerListener(this);

            //Realize the player
            player.realize();
            vc = (VolumeControl) player.getControl("VolumeControl");
            if(vc != null) {
                
            }
            vc.setMute(isMute);
            vc.setLevel(volumeLevel);

            player.prefetch();
			
			//Save name
			m_Music = fileName;
            //player.start();
        } catch (MediaException ex) {
            ex.printStackTrace();
        } catch (Throwable t) {
            reset();
        } finally{
            try {
				if(is != null)
					is.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }

    public void play(){
        try {
			if (player != null) player.start();
        } catch (MediaException ex) {
            ex.printStackTrace();
        }
    }

    public void stop(){
        try {
            defPlayer();
        } catch (MediaException ex) {
            ex.printStackTrace();
        }
        reset();
    }

    public void defPlayer() throws MediaException {
        if (player != null) {
            if(player.getState() == Player.STARTED) {
                player.stop();
            }
            if(player.getState() == Player.PREFETCHED) {
                player.deallocate();
            }
            if(player.getState() == Player.REALIZED || player.getState() == Player.UNREALIZED) {
                player.close();
            }
        }
        player = null;
    }

    public void reset() {
        player = null;
    }

    /**
     * @return the player
     */
    public Player getPlayer() {
        return player;
    }

    /**
     * @return the isMute
     */
    public boolean isIsMute() {
        return isMute;
    }

    /**
     * @param isMute the isMute to set
     */
    public void setIsMute(boolean mute) {
        isMute = mute;

		if(vc != null){
			vc.setMute(isMute);
		}
    }

    public void setVolumeLevel(int vLevel){
        volumeLevel = vLevel;

		if(vc != null)
			vc.setLevel(volumeLevel);
    }

    public int getVolumeLevel(){
        return volumeLevel;
    }
	
	public boolean isReady() {
		//If null, not ready
		if (player == null) return false;
		
		//Otherwise true if not started
		return player.getState() == Player.PREFETCHED;
	}
}
